package com.naiterui.ehp.bs.cms.modules.pharmacy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.pharmacy.entity.WarehouseGoods;
import com.naiterui.ehp.bs.cms.modules.pharmacy.mapper.WarehouseGoodsMapper;
import com.naiterui.ehp.bs.cms.modules.pharmacy.service.IWarehouseGoodsService;
import com.naiterui.ehp.bs.cms.modules.pharmacy.vo.WarehouseGoodsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class WarehouseGoodsServiceImpl extends ServiceImpl<WarehouseGoodsMapper, WarehouseGoods> implements IWarehouseGoodsService {

  @Override
  public PageVO<WarehouseGoodsVO> queryPage(PageParamsVO pageParamsVO, WarehouseGoodsVO warehouseGoodsVO) {
    QueryWrapper<WarehouseGoodsVO> entityWrapper = this.buildEntityWrapper(pageParamsVO, warehouseGoodsVO);
    Page<WarehouseGoodsVO> page = this.baseMapper.queryPage(pageParamsVO.getPage(), warehouseGoodsVO.getWarehouseId(), entityWrapper);
    return PageVO.toPageVo(page);
  }

  private QueryWrapper<WarehouseGoodsVO> buildEntityWrapper(PageParamsVO pageParamsVO, WarehouseGoodsVO warehouseGoodsVO) {

    QueryWrapper<WarehouseGoodsVO> entityWrapper = pageParamsVO.getEntityWrapper("t");

    String number = warehouseGoodsVO.getNumber();
    entityWrapper.eq(StringUtils.isNotEmpty(number), "t.number", number);

    String name = warehouseGoodsVO.getName();
    entityWrapper.like(StringUtils.isNotEmpty(name), "t.name", name);

    Long pharmacologyClassificationId = warehouseGoodsVO.getPharmacologyClassificationId();
    entityWrapper.eq(pharmacologyClassificationId != null, "pp.classification_id", pharmacologyClassificationId);

    return entityWrapper;
  }

  @Override
  public PageVO<WarehouseGoodsVO> selectList(PageParamsVO pageParamsVO, Long warehouseId) {
    Page<WarehouseGoodsVO> page = this.baseMapper.selectList(pageParamsVO.getPage(), warehouseId);
    return PageVO.toPageVo(page);
  }

}
